'''
Created on 2022年10月27日
pip install pyenchant
@author: zz
'''
import os, re
import enchant


if __name__ == '__main__':

    input_file = input('Please input file directory = ').strip()

    lines = []
    file_line = re.compile(r'.*\n')
    index = 0
    log_file = "./English_check.log"

    d = enchant.Dict("en_US")

    if os.path.isfile(input_file):
        
        log_file_w = open(log_file, mode = 'w', encoding = 'utf-8', newline="\n")
        log_file_w.write(f'======= {input_file} English_check start ===========')

        with open(input_file, mode = 'r', encoding = 'utf-8', errors = 'ignore', newline = '\n') as fh_r:
            file_content = fh_r.read() + '\n'
            # linesカウント
            for m in re.finditer(file_line, file_content):
                lines.append(m.group())

        for line in lines:
            print(line)
            result = ''.join(re.findall(r'[A-Za-z]'), line.lower())
            print(result)
            for word in result:
                if not d.check(word):
                    log_file_w.write(f'[line:{index}][{line}][{word}] spelling mistake \n')
                    log_file_w.write(f'the correct word could be [{d.suggest(word)}] \n')
                    
            index += 1
        
        
        log_file_w.write(f'======= {input_file} English_check end ===========')
    else:
        pass